﻿/* 
    Simple Power Option - Is the program to configure Power settings for MS Windows 7
    Copyright (C) 2011  Alexey Egorov <Computer.Pers AT gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Security;
using WinAPI;

namespace SimplePowerOptions
{

    public partial class MainF : Form
    {
        int FullPwerTimes = 0; //
        public MainF()
        {
            InitializeComponent();
            ReadAll();
            ShowInTaskbar = false;
        }
        /*
        public partial class Temperature
        {
            static public string getalltemp()
            {
                string result= string.Empty;
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_DesktopMonitor");
                foreach (ManagementObject obj in searcher.Get())
                {
                    result += " - " + obj.ToString();
                }
                return result;
            }
        }
        */
        

        public void ReadAll()
        {
            curpowerschemelabel.Text = NativeMethods.PowerGetCurrentSchemeName();
            Processortrackbar.Value = NativeMethods.PowerGetCurrentSchemeProcessorMAX();
            brightnesstrack.Value = NativeMethods.PowerGetCurrentSchemeBrightness();
            try
            {
                notifyIcon1.Text = "Процессор: " + Processortrackbar.Value.ToString() + "%\n" +
                                   "Яркость: " + brightnesstrack.Value.ToString() + "%\n";
            }
            catch { notifyIcon1.Text = Text; }
        }

        private void Processortrackbar_ValueChanged(object sender, EventArgs e)
        {
            maxprocessorlabel.Text = Processortrackbar.Value.ToString() + "%";
            if (NativeMethods.PowerSetCurrentSchemeProcessorMAX(Processortrackbar.Value) == 0)
            {
                if (!NativeMethods.PowerAllpyChanges()) 
                { 
                    MainF.ActiveForm.Text = "Setting Processor settings FAILED!"; 
                };
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ReadAll();
            if (ProcessorCheckBox.Checked) //If ENABLED auto processor power configuration.
            {
                if (Processortrackbar.Value >= 100) //checking processor power 100%
                {
                    FullPwerTimes++;
                }
                else
                {
                    FullPwerTimes = 0;
                }
                if (FullPwerTimes >= 60 * 10) //if Processor Power = 100% more then 10 minutes.
                {
                    Processortrackbar.Value--;
                    FullPwerTimes = 0;
                }
            }
        }

        private void brightnesstrack_ValueChanged(object sender, EventArgs e)
        {
            brightnesslabel.Text = brightnesstrack.Value.ToString() + "%";
            if (NativeMethods.PowerSetCurrentSchemeBrightness(brightnesstrack.Value) == 0)
            {                
                if (!NativeMethods.PowerAllpyChanges()) 
                {
                    MainF.ActiveForm.Text = "Setting Brightness settings FAILED!"; 
                };
            }

        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (Visible)
                {
                    Visible = !Visible;
                    SetFormPos();
                }
                else
                {
                    SetFormPos();
                    Visible = !Visible;
                }
            }
        }

        private void SetFormPos()
        {
            
            Point POS = new Point(System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y);
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);

            if (POS.Y <= rect.Top)
            {
                POS.Y = rect.Top + 10;
            }
            else
            {
                POS.Y = rect.Height - Size.Height - 10;
            }

            if (POS.X <= rect.Left)
            {
                POS.X = rect.Left + 10;
            }
            else if (POS.X >=rect.Width)
            {
                POS.X = rect.Width - Size.Width - 10;
            }

            Location = POS;
        }


        private void MainF_Shown(object sender, EventArgs e)
        {
            SetFormPos();
            this.TopMost = true;
        }

        private void MainF_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != System.Windows.Forms.CloseReason.ApplicationExitCall)
            {
                e.Cancel = true;
                Hide();
            }         
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox3_DoubleClick(object sender, EventArgs e)
        {
            Processortrackbar.Value = Processortrackbar.Maximum;
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            brightnesstrack.Value = brightnesstrack.Maximum;
        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            brightnesstrack.Value = brightnesstrack.Minimum;

        }

        private void pictureBox4_DoubleClick(object sender, EventArgs e)
        {
            Processortrackbar.Value = Processortrackbar.Minimum;
        }

        private void MainF_Load(object sender, EventArgs e)
        {
            ToolTip ProcessorTooltip = new ToolTip();

            // Set up the delays for the ToolTip.
            ProcessorTooltip.AutoPopDelay = 5000;
            ProcessorTooltip.InitialDelay = 1000;
            ProcessorTooltip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            ProcessorTooltip.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            ProcessorTooltip.SetToolTip(this.ProcessorCheckBox, "Автоматическое слежение за максимальной производительностью процессора.\nСнижает производительность процессора после 10 минут 100% работы.");
        }

        private void ProceccorCheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Processortrackbar_Scroll(object sender, EventArgs e)
        {

        }

    }
    
}
